package cn.keniykey.mc.landterritory.landview;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.UUID;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.keniykey.mc.generallibrary.input.api.EInputResult;
import cn.keniykey.mc.generallibrary.input.api.IInputRequestManager;
import cn.keniykey.mc.generallibrary.input.api.IInputResultHandler;
import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration.ViewConfig;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.api.ELandType;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.landterritory.vault.Econ;
import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;

public final class LandMainView extends AbLandFrameView{

	protected LandMainView(Player player1) {
		super(player1);
	}
	
	private static final String VIEW_NAME = "landmain";
	
	@Override
	protected void onCreate() {
		super.onCreate();
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		this.setViewType(EContainerType.matchContainer(config_view.getViewSize()));
		this.setTitle(Util.conver(config_view.getViewTitle()));
		
		ItemStack item = ViewsConfiguration.setItemMeta(config_view.getViewItem("buylandlimit"), null,
				Util.listFromStr(LanguageManager.getLang(1100001,ConfigurationManager.View_LandMain_LandLimit_Price)));
		this.setIconClick(config_view.getViewItemSlot("buylandlimit"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				IInputRequestManager.getIInputRequestManager().requestInput(player, "s", ConfigurationManager.View_Choose_Countdown, true, new IInputResultHandler() {
					@Override
					public boolean onInputResult(EInputResult result, OfflinePlayer offplayer, @Nullable String str) {
						boolean cancel = true;
						if(EInputResult.INPUT.equals(result)) {
							cancel = inputBuyLandLimit(player, str);
						}
						LandViewUtil.inputResultResponse(result, offplayer, cancel, 1100100);
						return cancel;
					}
				});
				player.sendMessage(LanguageManager.getChatLang(1100104, "s"));
				view.finishAll();
			}
		});
		this.setIconClick(config_view.getViewItemSlot("create"), config_view.getViewItem("create"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				LandMainView.this.showView(new FrameViewIntent(LandCreateView.class));
			}
		});
		this.setIconClick(config_view.getViewItemSlot("here"), config_view.getViewItem("here"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final TreeSet<Land> tree_lands = LandManager.getLandManager().getLandsInHash(player.getLocation());
				final ArrayList<Land> list_lands = new ArrayList<>(tree_lands.size());
				for(final Land land : tree_lands) list_lands.add(land);
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", list_lands);
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("sold"), config_view.getViewItem("sold"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final FrameViewIntent intent = new FrameViewIntent(LandSoldListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", LandManager.getLandManager().getSoldLands());
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("all_territory"), config_view.getViewItem("all_territory"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", LandManager.getLandManager().getServerLands(ELandType.TERRITORY));
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("all_community"), config_view.getViewItem("all_community"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", LandManager.getLandManager().getServerLands(ELandType.COMMUNITY));
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("all_ground"), config_view.getViewItem("all_ground"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", LandManager.getLandManager().getServerLands(ELandType.GROUND));
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("only_territory"), config_view.getViewItem("only_territory"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(player.getUniqueId())) {
					if(ELandType.TERRITORY.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("only_community"), config_view.getViewItem("only_community"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(player.getUniqueId())) {
					if(ELandType.COMMUNITY.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
		this.setIconClick(config_view.getViewItemSlot("only_ground"), config_view.getViewItem("only_ground"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandMainView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(player.getUniqueId())) {
					if(ELandType.GROUND.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandMainView.this.showView(intent);
			}
		});
	}
	
	@Override
	protected void onShowBefore() {
		super.onShowBefore();
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		final Player player = this.getPlayer();
		final UUID player_uuid = player.getUniqueId();
		
		ItemStack item = config_view.getViewItem("player");
		ItemMeta meta = item.getItemMeta();
		meta.setDisplayName(meta.getDisplayName() + player.getName());
		meta.setLore(Util.listFromStr(LanguageManager.getLang(1100000, LandManager.getLandManager().getPlayerLandLimit(player_uuid),
				LandUtil.getPlayerUsedLandLimit(player_uuid))));
		item.setItemMeta(meta);
		this.setIcon(config_view.getViewItemSlot("player"), item);
	}
	
	private static boolean inputBuyLandLimit(Player player,@Nullable String str) {
		if(str == null || !Pattern.matches("^\\d{1,2}$", str)) {
			LandViewUtil.sendInputMustPositiveNumber(player);
			return false;
		}
		if(!LandViewUtil.hasSendUsePermission(player, player.getWorld().getName(), ConfigurationManager.View_LandMain_LandLimit_Per, 1100100)) return true;
		final int amount = Integer.valueOf(str);
		if(amount < 1) {
			LandViewUtil.sendInputMustPositiveNumber(player);
			return false;
		}
		final UUID player_uuid = player.getUniqueId();
		final int amount_has = LandManager.getLandManager().getPlayerLandLimit(player_uuid);
		if(amount_has < 0) {
			player.sendMessage(LanguageManager.getChatLang(1100102));
			return true;
		}
		final int price_pay = (amount * ConfigurationManager.View_LandMain_LandLimit_Price);
		if(price_pay > 0 && !player.isOp() && !Econ.getEcon().take(player, price_pay)) {
			player.sendMessage(LanguageManager.getChatLang(1100101, str, price_pay));
			return true;
		}
		LandManager.getLandManager().setPlayerLandLimit(player_uuid, (amount_has + amount));
		player.sendMessage(LanguageManager.getChatLang(1100103, price_pay, amount));
		return true;
	}
}